package com.example.javahello.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class IndexController {

    public static Date startTime = new Date();


    @GetMapping("/index")
    public String index() {
        return "Hello, index page";
    }

    @GetMapping(value = "/")
    public String root() {
        StringBuilder sb = new StringBuilder();
        for (String info : getServiceInfo()) {
            sb.append(info);
            sb.append("<br>");
        }

        return sb.toString();
    }

    public String[] getServiceInfo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String my_node_name = System.getenv("MY_NODE_NAME");
        String my_node_ip = System.getenv("MY_NODE_IP");
        String my_pod_name = System.getenv("MY_POD_NAME");
        String my_pod_ip = System.getenv("MY_POD_IP");
        String my_pod_namespace = System.getenv("MY_POD_NAMESPACE");
        String my_pod_service_account = System.getenv("MY_POD_SERVICE_ACCOUNT");

        String[] infos = {
                "Hello, java spring boot",
                "<br>",
                "启动时间： " + sdf.format(startTime) ,
                "当前时间： " + sdf.format(new Date()),
                "<br>",
                "host name: " + my_node_name,
                "host ip  : " + my_node_ip,
                "<br>",
                "pod name : " + my_pod_name,
                "host ip  : " + my_pod_ip,
                "<br>",
                "namespace: " + my_pod_namespace,
                "service account: " + my_pod_service_account
        };
        return infos;
    }


}

